//var server = "http://fisher.yzboy.top" 
//var server = "http://localhost:8080";
var server="http://127.0.0.1:8080";
var token = "1";
var timestamp = (new Date()).valueOf();
var netWork = "";
 
function getNetwork() {
	var types = {};
	types[plus.networkinfo.CONNECTION_UNKNOW] = "未知网络";
	types[plus.networkinfo.CONNECTION_NONE] = "无网络";
	types[plus.networkinfo.CONNECTION_ETHERNET] = "以太网";
	types[plus.networkinfo.CONNECTION_WIFI] = "wifi";
	types[plus.networkinfo.CONNECTION_CELL2G] = "2G";
	types[plus.networkinfo.CONNECTION_CELL3G] = "3G";
	types[plus.networkinfo.CONNECTION_CELL4G] = "4G";
	var typeText = types[plus.networkinfo.getCurrentType()];
	netWork = typeText;
}
function ajaxPost(url, data, successCallBack, errorCallBack) {
	getNetwork();
	plus.nativeUI.showWaiting();
	var sign = md5(token + timestamp + "zhangfen"); 
	mui.ajax(server + url, {
		data: data,
		dataType: 'json',
		type: 'POST',
		headers: {
			"Accept": "*/*",
			"timestamp": timestamp,
			"sign": sign,
			"token": token,
			'Content-Type': 'application/json'
		},
		timeout: 20000,
		success: function(data) {
			plus.nativeUI.closeWaiting();
			//传入成功的函数，则执行函数
			if(typeof successCallBack === 'function') {
				successCallBack(data);
			}
		},
		error: function(xhr, type, errorThrown) {
			//"timeout", "error", "abort", "parsererror"、"null"
			plus.nativeUI.closeWaiting('', {
				back: "none"
			});
			//传入失败的函数，则执行函数
			if(typeof errorCallBack === 'function') {
				var errorInfo = {
					"xhr": xhr,
					"type": type,
					"errorThrown": errorThrown
				}
				errorCallBack(errorInfo);
			}
			console.log(type);  
			if(type == "abort") {
				if(netWork == "未知网络" || netWork == "无网络") {
					mui.alert("未连接网络，请开启网络服务后重试");
				} else {
					mui.alert("服务器抽风了，请稍后再试");
				}
			}  
			if(type == "timeout") {
				mui.alert("网络请求超时，请稍后再试");
			}
		}
	});
} 